var app = getApp();
//全新用户
const AUTH_USER_TYPE_NEW = 1;
//删除小程序再次进入的用户
const AUTH_USER_TYPE_OLD_DEL = 2;
//老用户
const AUTH_USER_TYPE_OLD = 3;
Component({
    properties: {
    },
    data:{
        assets:app.assets,
        assetsByEnv:app.assetsByEnv,
        //标记用户是否点击了登录按钮，点击后将视图移动到不可见区域
        isTriggerAuth:false,
        //用于标记是否弹出授权,默认不弹出
        user:true
    },
    ready:function(){
        var that = this;
        var user = wx.getStorageSync('user');
        if(!user){
            app.getUserInfo(null,function(userInfo){
                user = wx.getStorageSync('user');
                //删除小程序重新进入老客户
                if(user) {
                    that.triggerEvent('authSuccess',{userType:AUTH_USER_TYPE_OLD_DEL});
                //全新用户
                }else {
                    //唤起授权视图
                    that._updateUserForAuthView();
                }
            });
        //已经授权登录过的老用户
        } else {
            that.triggerEvent('authSuccess',{userType:AUTH_USER_TYPE_OLD});
        }

    },
    methods: {
        closeAuth(){
            this.setData({
              isTriggerAtuh:true
            });
        },
        //更新user，用于判断是否要弹出授权视图
        _updateUserForAuthView(){
            var user = wx.getStorageSync('user');
            this.setData({
                user:user
            });
        },
        bindGetUserInfo(e){
            var that = this;
            var user = wx.getStorageSync('user');
            if(!user) {
                //调用应用实例的方法获取全局数据
                if(e.detail.errMsg == 'getUserInfo:ok') {
                    app.getUserInfo(e.detail,function(userInfo){
                        that.triggerEvent('authSuccess',{userType:AUTH_USER_TYPE_NEW});
                        //成功后关闭授权视图
                        this._updateUserForAuthView();
                    });
                }else{
                    this.setData({
                        isTriggerAtuh:false
                    });
                }

            }
        },
    }
});